package cn.yunhe.controll;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import cn.yunhe.model.House;
import cn.yunhe.model.Street;
import cn.yunhe.model.Type;
import cn.yunhe.model.UserRegs;
import cn.yunhe.services.SerRent;




public class FaBuAction extends BaseAction {

	
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		private String user_id; //用户
		private String type_id; //户型
		private String title;   //主题
		private String description;//描述
		private String price;// 价格
		private String floorage;//面积
		private String pubdate;//房产证日期
		private String contact;//联系方式
		private String street_id;//街道id
		
	     private File upload;
		 private String uploadContentType;
		 private String uploadFileName;
		 private String savePath;

		 SerRent serRent = new SerRent();
		 public String execute() throws Exception {
			    String path = getSavePath()+"\\"+getUploadFileName(); 
			    doUpload(upload, path);
			    
			      SerRent serRent =new SerRent();
			    
			      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			      
			      Date date=sdf.parse(pubdate);
			    		  
			      
			      Street street = serRent.selIdByStreet(street_id);
			      
			      Type type= serRent.selIdByType(type_id);
			      UserRegs userRegs =(UserRegs) getSession().getAttribute("userRegs");
			      
			      double pri=Double.parseDouble(price); 
			      
			      int floorag = Integer.parseInt(floorage);
			     
			      House house= new House(userRegs,type,title, description, new BigDecimal(pri),date,floorag, contact, street, uploadFileName);  

				 int falg = serRent.addHouse(house);
				 if(falg>0) {
					
					return SUCCESS;
				}else {
					 return ERROR;
				}
				 
				
			}	

			/*
			 * 上传方法
			 * */
				private void doUpload(File file, String uploadPath) throws Exception {
				FileOutputStream fos=new FileOutputStream(uploadPath);
				FileInputStream fis = new FileInputStream(file);	
			    byte[] b = new byte[1024];
			    int len =0;
			    while((len=fis.read(b))!=-1) {
			    	fos.write(b, 0, len);
			      }
				fis.close();
				fos.close();
				}

				public String getUser_id() {
					return user_id;
				}

				public void setUser_id(String user_id) {
					this.user_id = user_id;
				}

				public String getType_id() {
					return type_id;
				}

				public void setType_id(String type_id) {
					this.type_id = type_id;
				}

				public String getTitle() {
					return title;
				}

				public void setTitle(String title) {
					this.title = title;
				}

				public String getDescription() {
					return description;
				}

				public void setDescription(String description) {
					this.description = description;
				}

				public String getPrice() {
					return price;
				}

				public void setPrice(String price) {
					this.price = price;
				}

				public String getPubdate() {
					return pubdate;
				}

				
				
				

				public void setPubdate(String pubdate) {
					this.pubdate = pubdate;
				}

				public String getFloorage() {
					return floorage;
				}

				public void setFloorage(String floorage) {
					this.floorage = floorage;
				}
				
				
				public String getContact() {
					return contact;
				}

				public void setContact(String contact) {
					this.contact = contact;
				}

				
				
				public String getStreet_id() {
					return street_id;
				}

				public void setStreet_id(String street_id) {
					this.street_id = street_id;
				}

				public File getUpload() {
					return upload;
				}

				public void setUpload(File upload) {
					this.upload = upload;
				}

				public String getUploadContentType() {
					return uploadContentType;
				}

				public void setUploadContentType(String uploadContentType) {
					this.uploadContentType = uploadContentType;
				}

				public String getUploadFileName() {
					return uploadFileName;
				}

				public void setUploadFileName(String uploadFileName) {
					this.uploadFileName = uploadFileName;
				}

				public String getSavePath() {
					return ServletActionContext.getServletContext().getRealPath(savePath);
				}

				public void setSavePath(String savePath) {
					this.savePath = savePath;
				}

				
 }
